<?php
/**
 * Created by PhpStorm.
 * User: bzyy
 * Date: 2015-8-20 16:55:59
 * 对记录的处理
 *
 *
 *
 */
class Frm_log_actionModel extends BaseModel {


    protected $table_log;
    protected $table_action;

    protected $cookiename = 'SHTX';
    protected $overtime = 0;//过期时间

    function __construct($params, $db)
    {
        parent::__construct($params, $db);

        $this->table_log = $this->_prefix.'frm_log';
        $this->table_action = $this->_prefix.'frm_action';

        $this->overtime = time()+86400*30;
    }


    /**
     * 初始化写入的操作
     * 记录到对应的cookie中
     * @auth bzyy
     * 2015-8-20 17:54:01
     */
    public function initoutlink()
    {

        $par_url = array();
        parse_str($_SERVER['QUERY_STRING'],$par_url);

        if(!isset($par_url['frm_m']) || empty($par_url['frm_m']) || !is_string($par_url['frm_m']))
        {
            return false;
        }

        $frm_m = strval($par_url['frm_m']);
        $frm_t = strval($par_url['frm_t']);
        $frm_i = strval($par_url['frm_i']);

        //计算正常的url
        $filter_keys = array('frm_m','frm_t','frm_i');

        $n_par_url_arr = array();
        foreach($par_url as $key=>$val)
        {
            if(in_array($key,$filter_keys)){
                continue;
            }
            $n_par_url_arr[$key] = $val;
        }

        //$frm_u = $_SERVER['SCRIPT_NAME'].'?'.http_build_query($n_par_url_arr);

        $frm_u = strval($_SERVER['HTTP_REFERER']);

        $data = array();
        $info = $this->getCookieId();
        if(!empty($info) && $info['frm_m']==$frm_m && $info['frm_t']==$frm_t && $info['frm_i']==$frm_i && $info['frm_u']==$frm_u){
            $data['frm_id'] = $info['frm_id'];
        }else{
            $data['frm_id'] = 'PC_'.uniqid(rand(0,99999).'_');
            $data['frm_m'] = $frm_m;
            $data['frm_t'] = $frm_t;
            $data['frm_i'] = $frm_i;
            $data['frm_u'] = $frm_u;
            $data['frm_time'] = time();
            $data['ip'] = get_ip();

            $insert_info = $this->_getInsertInfo($data);
            $this->db->query("INSERT INTO {$this->table_log}{$insert_info['fields']} VALUES{$insert_info['values']}");
            $insert_id = $this->db->insert_id();
        }
        //$n = setcookie($this->cookiename,$data['frm_id'],$this->overtime,'','',true,true);
        setcookie($this->cookiename,$data['frm_id'],$this->overtime,null,null,false,true);
        $_COOKIE[$this->cookiename] = $data['frm_id'];

        return $data['frm_id'];

    }

    /**
     * @param $action 统计名称
     * @param $action_id 统计对应的id
     *
     * 写入action统计数据
     *
     */
    public function writeAction($action,$action_id=0)
    {
        //读取名称
        $info = $this->getCookieId();
        if($info===false){
            return false;
        }
        //写入信息
        $data = array();
        $data['frm_id'] = $info['frm_id'];
        $data['action'] = $action;
        $data['action_id'] = $action_id;

        $data['ip'] = get_ip();
        $data['user_id'] = $this->getuserid();
        $data['time'] = time();
        $data['url'] = $this->getnowurl();
        $data['sourceurl'] = strval($_SERVER['HTTP_REFERER']);

        $insert_info = $this->_getInsertInfo($data);
        $this->db->query("INSERT INTO {$this->table_action}{$insert_info['fields']} VALUES{$insert_info['values']}");
        $insert_id = $this->db->insert_id();
        return true;
    }


    /**
     * @return string
     * 取得对应的cookie信息
     */
    protected function getCookieId(){
        $str = isset($_COOKIE[$this->cookiename]) && is_string($_COOKIE[$this->cookiename])?$_COOKIE[$this->cookiename]:'';

        //检测数据库中是否有此记录
        $sql = "select * from ".$this->table_log." where frm_id = '{$str}' ";
        $row = $this->getRow($sql);
        if(empty($row))
        {
            //清空记录
            setcookie($this->cookiename,'',30);
            return false;
        }

        return $row;
    }

    /**
     * 访问产生一条记录
     */
    public function visit(){


        if($this->is_ajax()){
            return false;
        }
        //读取名称
        $info = $this->getCookieId();
        if($info===false || empty($info)){
            return false;
        }

        //写入信息
        $data = array();
        $data['frm_id'] = $info['frm_id'];
        $data['action'] = 'visit';
        $data['action_id'] = 0;
        $data['ip'] = get_ip();
        $data['user_id'] = $this->getuserid();
        $data['time'] = time();
        $data['url'] = $this->getnowurl();
        $data['sourceurl'] = strval($_SERVER['HTTP_REFERER']);

        $insert_info = $this->_getInsertInfo($data);
        $flag = $this->db->query("INSERT INTO {$this->table_action}{$insert_info['fields']} VALUES{$insert_info['values']}");

        return $flag;

    }

    /**
     * 取得当前的完整url
     */
    protected function getnowurl(){
        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        return $url;
    }

    public function getuserid(){
        $user_id = 0;

        if(isset($_SESSION['user_info']) && is_array($_SESSION['user_info']) && isset($_SESSION['user_info']['user_id']))
        {
            $user_id = intval($_SESSION['user_info']['user_id']);
        }

        return $user_id;
    }

    public function is_ajax() {
        if ($_SERVER['HTTP_X_REQUESTED_WITH'] && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') return true;

        return false;
    }



}